
***********CALCULATE MONTHLY S&P CONVEXITY*************
est clear

use "$jfqa_rep/CRSP_Daily_Index.dta", clear

qui{
drop if missing(spindx) 

sort year month day

gen monthyear=ym(year, month)

*assign first and last observation of month
gen prc_first=spindx if month~=month[_n-1]
egen min_prc=min(prc_first), by(year month)
replace prc_first=min_prc if missing(prc_first)
drop min_prc

gen prc_last=spindx if month~=month[_n+1]
egen min_prc=min(prc_last), by(year month)
replace prc_last=min_prc if missing(prc_last)
drop min_prc

sort year month
egen avgprc=mean(spindx), by(year month)
egen stdprc=sd(spindx), by(year month)
gen convexity=((prc_first+prc_last)/2-avgprc)/((prc_first+prc_last)/2)

drop if monthyear==monthyear[_n+1]

keep month year convexity
}

save "$jfqa_rep/TIA1_SP_Convexity.dta", replace


****** TABLE IA1 ESTIMATES*****
import delimited "$jfqa_rep/Goyal_Excel_Monthly.csv", clear

qui{
rename index sp_index 
rename d12 sp_div
rename e12 sp_earn

drop if missing(sp_index)

destring sp_index, force replace

merge 1:1 year month using "$jfqa_rep/CRSP_Monthly_Index.dta"
keep if _merge==3
drop _merge

gen monthyear=ym(year, month)
tsset monthyear
sort monthyear

gen log_prem=(crsp_spvw-log(1+rf))*100 

merge 1:1 year month using "$jfqa_rep/TIA1_SP_Convexity.dta"
keep if _merge==3
drop _merge
sort monthyear

rm "$jfqa_rep/TIA1_SP_Convexity.dta"

order year month monthyear log_prem

*construct control variables
gen dp = log(sp_div/sp_index)
gen dy = log(sp_div/L.sp_index)
gen ep = log(sp_earn/sp_index)
gen de = log(sp_div/sp_earn)
replace svar=svar
replace bm=bm
replace ntis=ntis
*gen beta_prem=csp
replace tbl=tbl
replace lty=lty
replace ltr=ltr
gen tms=lty-tbl
gen dfy = aaa-baa
gen dfr=corpr-ltr
replace infl=infl

drop if year<1963
drop if year==1963 & month<6

*standardized variables
foreach i in convexity log_prem dp dy ep de bm svar ntis tbl lty ltr tms dfy dfr infl {
egen `i'_std=std(`i') 
}

drop if year==2022 & month==12
}

sum convexity, d

corr convexity log_prem dp dy ep de bm svar ntis tbl lty ltr tms dfy dfr infl
